//
//  ZFBMomentCell.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/30.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "ZFBMomentCell.h"
#import <Masonry.h>
#import "ZFBMomentModel.h"
@interface ZFBMomentCell ()
@property (nonatomic, strong) UIImageView *iconImagView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) MASConstraint *constraint;

@end

@implementation ZFBMomentCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI{
    self.iconImagView = [[UIImageView alloc]init];
    self.iconImagView.layer.cornerRadius = 5;
    self.iconImagView.clipsToBounds = YES;
    [self.contentView addSubview:self.iconImagView];
    
    [self.iconImagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.font = [UIFont systemFontOfSize:11];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).and.offset(8);
        make.left.equalTo(self.iconImagView.mas_right).offset(8);
        make.right.mas_lessThanOrEqualTo(self.contentView.mas_right).offset(-10);
    }];
    
    self.contentLabel = [[UILabel alloc]init];
    self.contentLabel.numberOfLines = 0;//多行显示
    self.contentLabel.font = [UIFont systemFontOfSize:13];
    [self.contentLabel sizeToFit];
    [self.contentView addSubview:self.contentLabel];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(8);
        make.left.equalTo(self.nameLabel).offset(8);
        make.right.lessThanOrEqualTo(self.contentView.mas_right).offset(-8);
//        make.bottom.equalTo(self.contentView.mas_bottom).offset(-8);
    }];
    
    self.pictureView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.pictureView];
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(8);
        make.left.equalTo(self.contentLabel);
        make.size.mas_equalTo(CGSizeMake(10, 10));
//        make.bottom.equalTo(self.contentView.mas_bottom).offset(-8);
    }];
    
    self.timeLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel sizeToFit];
    self.timeLabel.font = [UIFont systemFontOfSize:10];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pictureView.mas_bottom).offset(8);
        make.left.equalTo(self.pictureView);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-8); //此处就是自适应的关键
    }];
    
    
//    NSLog(@"nihao!!");
    UIButton *deleteButton = [[UIButton alloc]init];
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [deleteButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    deleteButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [self.contentView addSubview:deleteButton];
    self.deleteButton = deleteButton;
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel.mas_right).offset(8);
        make.baseline.equalTo(self.timeLabel);
    }];
//
    UIButton *commentButton = [[UIButton alloc]init];
    [commentButton setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [self.contentView addSubview:commentButton];
    
    [commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-8);
        make.top.equalTo(deleteButton);
    }];
}
- (void)setModel:(ZFBMomentModel *)model{
    _model = model;
    self.iconImagView.image = [UIImage imageNamed:model.icon];
    self.nameLabel.text = model.name;
    self.contentLabel.text = model.content;
    //此处需要进行判断是否存在图片
    UIImage *image = [UIImage imageNamed:model.picture];
    self.pictureView.image = image;
    CGFloat wight = 150;
    CGFloat height = 150 / image.size.width * image.size.height;
    //此处更新约束
    [self.pictureView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(wight, height));
    }];
    self.timeLabel.text = model.time;
    
    if (model.isMine) {
        self.deleteButton.hidden = NO;
    }else{
        self.deleteButton.hidden = YES;
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
